﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LEARNREN.COM.Entity;
using LEARNREN.COM.TOOL;

namespace LEARNREN.COM.UI.LabelProviders
{
    /// <summary>
    /// 文章属性标签
    /// </summary>
    class ATPLabel : AbstractLabelProvider
    {

        public ATPLabel(string label)
            : base(label)
        {

        }

        public override string GetHtml()
        {
            try
            {
                string[] paras = LabelProviderHelper.GetProperties(this.LabelString);

                // 没有参数则返回原文
                if (paras.Length < 1)
                {
                    return this.LabelString;
                }

                // 如果上下文不是文章则返回原文
                if (!(this.Context.State is Article))
                {
                    return this.LabelString;
                }

                string prop = paras[0];
                switch (prop.ToLower())
                {
                    case "url":
                        return this.GroupLogicFactory.ArticleLogic.GetURL(this.Context.State as Article);
                    case "title":
                        string value = typeof(Article).GetProperty(prop).GetValue(this.Context.State, null).ToString();
                        if (paras.Length > 1)
                        {
                            int l = 0;
                            if (int.TryParse(paras[1], out l))
                            {
                                return value.ToShortFormat(l);
                            }
                            else
                            {
                                return value;
                            }
                        }
                        else
                        {
                            return value;
                        }
                    default:
                        return typeof(Article).GetProperty(prop).GetValue(this.Context.State, null).ToString();
                }

            }
            catch
            {
                return this.LabelString;
            }
        }
    }
}
